<script type="text/javascript">
    $(document).ready(function(){
        $('#edit_hot_product_form').dialog({
            'autoOpen':false,
            'width':600,
            'height':438,
            'resizable':false,
            'modal':true,
            'title': 'Change hot image',
            'dialogClass':'fixed-dialog',
        });
        $('.list_hot_product').on('click','.edit_hot_product',function(){
            var productUri = $(this).closest('tr').attr('val');
            $.post('~product/loadImageOfProduct',{productUri : productUri},function(data){
                $('#edit_hot_product_form').html(data);
                $('#edit_hot_product_form').dialog('open');
                $('.image_tool').css('right','-200px');
                $('.image_tool').css('display','block');
            });
        }); 
        $('.list_hot_product').on('click','.remove_product',function(){
            var productUri = $(this).closest('tr').attr('val');
            var dis = $(this);
            $.post('~product/deleteProduct',{productUri : productUri},function(data){
                if(data.indexOf('FALSE') == -1){
                    alert("Xoa thanh cong!");
                    dis.closest('tr').fadeOut(500,function(){
                        $(this).remove(); 
                    });
                }
            });
        }); 

    });
</script>

<div class="admin_header_title">PRODUCT MANAGE</div>
<div class="metro_table green_metro_style">
    <div class="metro_table_title"><i class="fa fa-bars"></i> List Products</div>
    <div class="metro_table_content">
        <div class="metro_page_number">
            <ul>
                <?php
                    $iBegin = ($template->get('page') <= 5 ? 0 : $template->get('page') - 5);
                    echo '<li '.($template->get('page') == 1 ? 'class="page_disable"' : '').' ><a '.($template->get('page') > 1 ? 'href="?page='.($template->get('page')-1).'"' : '').' ><i class="fa fa-long-arrow-left"></i> Prev</a></li>';
                    $i = 0;
                    for($i = $iBegin; $i<= $template->get('numberPage'); $i++)
                    {
                        echo '<li '.($i + 1 == $template->get('page') ? 'class="page_active" ' : '').'><a href="?page='.($i + 1).'">'.($i + 1).'</a></li>';
                        if($i - $iBegin == 4)
                            break;
                    }
                    $i +=1;
                    echo '<li '.($template->get('page') >= $template->get('numberPage') ? 'class="page_disable"' : '').' ><a '.($i < $template->get('numberPage') ? 'href="?page='.($i + 1).'"' : '').'>Next <i class="fa fa-long-arrow-right"></i></a></li>';
                ?>
                
            </ul>
        </div>
        <table id="table_hot_product" class="list_hot_product">
            <thead>
                <tr>
                    <th style="width: 60px;"><i class="fa fa-picture-o"></i> Image</th>
                    <th style="width: 100px;"><i class="fa fa-tag"></i> Product ID</th>
                    <th style="width: 100px;"><i class="fa fa-info"></i> Name</th>
                    <th style="width: 80px;"><i class="fa fa-clock-o"></i> Time</th>
                    <?php if(database::isAdmin()){ echo '<th><i class="fa fa-user"></i> OwnUser</th>';} ?>
                    <th style="width:80px;"><i class="fa fa-info"></i> Category</th>
                    <th style="width:300px;"><i class="fa fa-info"></i> Description</th>
                    <th style="width:100px;"></th>
                </tr>
            </thead>
            <tbody>
                <?php
                    if($template->get('aProducts'))
                    {
                        foreach($template->get('aProducts') as $aProduct)
                        {
                            $aParams = explode('/',$aProduct['uri']);
                            echo '<tr val="'.$aProduct['uri'].'">';
                            echo '<td class="avatar_cell"><img src="'.URL::getLink('upload/'.(isset($aProduct['image']) ? $aProduct['image'] : $aProduct['defaultImage'])).'"></td>';
                            echo '<td><a href="'.$aProduct['uri'].'">'.end($aParams).'</a></td>';
                            echo '<td><a href="'.$aProduct['uri'].'">'.$aProduct['name'].'</a></td>';
                            echo '<td>'.date('d/m/Y' , $aProduct['time']).'</td>';
                            if(database::isAdmin())
                            {
                                echo '<td><a href="'.$aProduct['userUri'].'">'.$aProduct['userName'].'</a></td>';
                            }
                            echo '<td><a href="'.$aProduct['categoryUri'].'">'.$aProduct['categoryName'].'</a></td>';
                            echo '<td class="description_cell"><div>'.$aProduct['description'].'</div></td>';
                            echo '<td><button class="edit_hot_product table_bt_metro_edit"><i class="fa fa-pencil"></i></button><button class="remove_product table_bt_metro_delete"><i class="fa fa-trash-o"></i></button></td>';
                            echo '</tr>';
                        }
                    }
                ?>
            </tbody>
        </table>
    </div>
</div>

<div id="edit_hot_product_form"></div>